package com.lbs.jiepiaisi.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView.Adapter;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lbs.jiepiaisi.R;
import com.lbs.jiepiaisi.bean.WorkOrderListBean;
import com.lbs.jiepiaisi.utils.Constant;

import java.util.List;

/**
 *
 * 时间：2018/1/25 10:38
 * 作用：工单列表适配器
 */

public class WorkOrderListAdapter extends Adapter<ViewHolder> {
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;
    private Context context;
    List<WorkOrderListBean.Result.Item> listItem;
    private String type;
    private boolean isLoadMore = true;

    public WorkOrderListAdapter(Context context, List<WorkOrderListBean.Result.Item> listItem,String type) {
        this.context = context;
        this.listItem = listItem;
        this.type = type;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position,String flag);

        void onActionClick(View view, int position,String flag);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemCount() {
        return listItem.size() == 0 ? 0 : listItem.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        // 设置最后一个item
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    /**
     * 创建新View，被LayoutManager所调用
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(context).inflate(R.layout.list_item_work_order_list, parent,
                    false);

            return new ItemViewHolder(view);
        } else if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_foot, parent,
                    false);
            return new FootViewHolder(view);
        }
        return null;
    }

    /**
     * 把现在的数据绑定到原有的viewHolder之上
     */
    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final int sel = position;
        if (holder instanceof ItemViewHolder) {
            //设置工单编号
            ((ItemViewHolder) holder).mItemWorkOrderNum.setText(listItem.get(position).getWorkOrderNo());
            //设置工单类型
            final String wType  = listItem.get(position).getWordOrderType();
            if("0".equals(wType)){
                ((ItemViewHolder) holder).mItemWorkOrderType.setText("新装");
            }else if("1".equals(wType)){
                ((ItemViewHolder) holder).mItemWorkOrderType.setText("维护");
            }else if("2".equals(wType)){
                ((ItemViewHolder) holder).mItemWorkOrderType.setText("拆机");
            }else if("3".equals(wType)){
                ((ItemViewHolder) holder).mItemWorkOrderType.setText("移机");
            }else if("4".equals(wType)){
                ((ItemViewHolder) holder).mItemWorkOrderType.setText("家访");
            }

            //设置车架号
            ((ItemViewHolder) holder).mItemWorkOrderVin.setText(listItem.get(position).getVin());


            //点击进入拍照
            ((ItemViewHolder) holder).mLinEnter.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("WorkOrderListAdapter","WorkId : "+listItem.get(sel).getWoId()+"");
                    if(onItemClickListener != null){
                        onItemClickListener.onItemClick(v,sel,wType);
                    }
                }
            });
            if("4".equals(wType)){
                ((ItemViewHolder) holder).mItemTvEnter.setText("进入家访");
            }else {
                ((ItemViewHolder) holder).mItemTvEnter.setText("进入拍照");
            }
            if(type.equals(Constant.WORK_ORDER_NO_GET)){
                ((ItemViewHolder) holder).mItemTvEnter.setText("接受工单");
                ((ItemViewHolder) holder).mItemTvEnter.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(onItemClickListener != null){
                            onItemClickListener.onActionClick(v,sel,type);
                        }
                    }
                });
            }
            if("4".equals(wType)){
                ((ItemViewHolder) holder).mLinEnter.setVisibility(View.GONE);
                ((ItemViewHolder) holder).mWorkOrderVin.setText("家访类型：");
                String homeVistype = listItem.get(sel).getHomeVisitsType();
                if("1".equals(homeVistype)){
                    ((ItemViewHolder) holder).mItemWorkOrderVin.setText("家庭拜访");
                }else {
                    ((ItemViewHolder) holder).mItemWorkOrderVin.setText("员工公司拜访");
                }

            }else {
                ((ItemViewHolder) holder).mWorkOrderVin.setText("车架号：");
                //改为大写  后台改改不就行了  还得让前端改
                ((ItemViewHolder) holder).mItemWorkOrderVin.setText(listItem.get(position).getVin().toUpperCase());
            }
            if(type.equals(Constant.WORK_ORDER_HISTORY_GET)){
                ((ItemViewHolder) holder).mLinEnter.setVisibility(View.GONE);
            }else {
                ((ItemViewHolder) holder).mLinEnter.setVisibility(View.VISIBLE);
            }
        } else if (holder instanceof FootViewHolder) {
            if (listItem.size() % 5 == 0 && isLoadMore) {//没有更多数据（总条数/一次加载的条数）
                ((FootViewHolder) holder).mProgressBar.setVisibility(View.VISIBLE);
                ((FootViewHolder) holder).mTextView.setVisibility(View.VISIBLE);
                ((FootViewHolder) holder).mNullTextView.setVisibility(View.GONE);
            } else {
                ((FootViewHolder) holder).mProgressBar.setVisibility(View.GONE);
                ((FootViewHolder) holder).mTextView.setVisibility(View.GONE);
                ((FootViewHolder) holder).mNullTextView.setVisibility(View.VISIBLE);
            }
        }
    }


    static class ItemViewHolder extends ViewHolder {
        //工单类型
        TextView mItemWorkOrderType;
        //设备编号
        TextView mItemWorkOrderNum;
        //车架号
        TextView mItemWorkOrderVin;
        //头部
        LinearLayout mLinTop;
        //进入拍照
        LinearLayout mLinEnter;
        //进入拍照文字
        TextView mItemTvEnter;
        //描述
        TextView mWorkOrderVin;



        public ItemViewHolder(View view) {
            super(view);
            mItemWorkOrderType = (TextView)view.findViewById(R.id.item_work_order_type);
            mItemWorkOrderNum = (TextView)view.findViewById(R.id.item_work_order_num);
            mItemWorkOrderVin = (TextView)view.findViewById(R.id.item_work_order_vin);
            mLinTop = (LinearLayout) view.findViewById(R.id.lin_top);
            mLinEnter = (LinearLayout) view.findViewById(R.id.lin_enter);
            mItemTvEnter = (TextView)view.findViewById(R.id.item_tv_enter);
            mWorkOrderVin = (TextView)view.findViewById(R.id.work_order_vin);
        }
    }

    public void setLoadMore(boolean loadMore) {
        isLoadMore = loadMore;
    }

    static class FootViewHolder extends ViewHolder {

        ProgressBar mProgressBar;
        TextView mTextView;
        TextView mNullTextView;

        public FootViewHolder(View view) {
            super(view);
            mProgressBar = (ProgressBar) view.findViewById(R.id.progressBar);
            mTextView = (TextView) view.findViewById(R.id.text_tv);
            mNullTextView = (TextView) view.findViewById(R.id.text_null);
        }
    }
}
